library(pixmap)
library(magrittr)
library(EBImage)
library(image.LineSegmentDetector)
library(image.ContourDetector)
library(image.CannyEdges)

setwd("~/Zellkraftwerk/packages/RJobTissueArea")

file <- file.path("inst/data","data_sum_M1730408_pos11.csv")

data.sum <- file%>%
  data.table::fread()

m.data_sum <- data.sum%>%
  as.matrix()

#cellres: pixel resolution in horizontal and vertical direction

image <-pixmapGrey(m.data_sum,cellres=c(1,1))

plot(image)

EBImage

constant threshold

grid <- expand.grid(
  sigma = c(1,2,3,4,5,10,100),
  threshold = seq(0,10,1)
)
grid$min <- NA
grid$max <- NA

img2 <- image@grey * 255
# Set up plots for 96 dpi images
  #library(EBImage)
  #dm <- dim(img2)/96
  #dev.new(width = dm[1], height = dm[2])
# Low pass filter with gblur and make binary

  par(mfrow=c(1,2))
  
for(i in 1:dim(grid)[1]){

    writeLines(c(
    paste0("- sigma: ",grid$sigma[i]),
    paste0("- threshold: ", grid$threshold[i])
  ))

  #Low-pass Gaussian filter
  xb <- EBImage::gblur(img2, grid$sigma[i])
  xb <- round(xb,digits = 0)
  #plot(xb)

  image2 <- pixmapGrey(xb,cellres=c(1,1)) #(xb-min(xb))/(max(xb)-min(xb))

  plot(image2)

  grid$min[i] <- min(xb)
  grid$max[i] <- max(xb)
  
  #threshold filtering
  pos <- which(xb > grid$threshold[i])
  xt <- xb
  xt[which(xb > grid$threshold[i])] <- 1
  xt[which(xb <= grid$threshold[i])] <- 0
  
 #xt <- EBImage::thresh(xb, w=grid$w[i],
 #                      h=grid$w[i],
 #                      offset = grid$offset[i])
 #plot(xt)

 image3 <- image
 image3@grey <- xt

 plot(image3)
}
## - sigma: 1
## - threshold: 0

## - sigma: 2
## - threshold: 0

## - sigma: 3
## - threshold: 0

## - sigma: 4
## - threshold: 0

## - sigma: 5
## - threshold: 0

## - sigma: 10
## - threshold: 0

## - sigma: 100
## - threshold: 0

## - sigma: 1
## - threshold: 1

## - sigma: 2
## - threshold: 1

## - sigma: 3
## - threshold: 1

## - sigma: 4
## - threshold: 1

## - sigma: 5
## - threshold: 1

## - sigma: 10
## - threshold: 1

## - sigma: 100
## - threshold: 1

## - sigma: 1
## - threshold: 2

## - sigma: 2
## - threshold: 2

## - sigma: 3
## - threshold: 2

## - sigma: 4
## - threshold: 2

## - sigma: 5
## - threshold: 2

## - sigma: 10
## - threshold: 2

## - sigma: 100
## - threshold: 2

## - sigma: 1
## - threshold: 3

## - sigma: 2
## - threshold: 3

## - sigma: 3
## - threshold: 3

## - sigma: 4
## - threshold: 3

## - sigma: 5
## - threshold: 3

## - sigma: 10
## - threshold: 3

## - sigma: 100
## - threshold: 3

## - sigma: 1
## - threshold: 4

## - sigma: 2
## - threshold: 4

## - sigma: 3
## - threshold: 4

## - sigma: 4
## - threshold: 4

## - sigma: 5
## - threshold: 4

## - sigma: 10
## - threshold: 4

## - sigma: 100
## - threshold: 4

## - sigma: 1
## - threshold: 5

## - sigma: 2
## - threshold: 5

## - sigma: 3
## - threshold: 5

## - sigma: 4
## - threshold: 5

## - sigma: 5
## - threshold: 5

## - sigma: 10
## - threshold: 5

## - sigma: 100
## - threshold: 5

## - sigma: 1
## - threshold: 6

## - sigma: 2
## - threshold: 6

## - sigma: 3
## - threshold: 6

## - sigma: 4
## - threshold: 6

## - sigma: 5
## - threshold: 6

## - sigma: 10
## - threshold: 6

## - sigma: 100
## - threshold: 6

## - sigma: 1
## - threshold: 7

## - sigma: 2
## - threshold: 7

## - sigma: 3
## - threshold: 7

## - sigma: 4
## - threshold: 7

## - sigma: 5
## - threshold: 7

## - sigma: 10
## - threshold: 7

## - sigma: 100
## - threshold: 7

## - sigma: 1
## - threshold: 8

## - sigma: 2
## - threshold: 8

## - sigma: 3
## - threshold: 8

## - sigma: 4
## - threshold: 8

## - sigma: 5
## - threshold: 8

## - sigma: 10
## - threshold: 8

## - sigma: 100
## - threshold: 8

## - sigma: 1
## - threshold: 9

## - sigma: 2
## - threshold: 9

## - sigma: 3
## - threshold: 9

## - sigma: 4
## - threshold: 9

## - sigma: 5
## - threshold: 9

## - sigma: 10
## - threshold: 9

## - sigma: 100
## - threshold: 9

## - sigma: 1
## - threshold: 10

## - sigma: 2
## - threshold: 10

## - sigma: 3
## - threshold: 10

## - sigma: 4
## - threshold: 10

## - sigma: 5
## - threshold: 10

## - sigma: 10
## - threshold: 10

## - sigma: 100
## - threshold: 10

grid
##    sigma threshold min max
## 1      1         0   0 200
## 2      2         0   0 177
## 3      3         0   0 149
## 4      4         0   0 125
## 5      5         0   0 110
## 6     10         0   0  91
## 7    100         0   2  47
## 8      1         1   0 200
## 9      2         1   0 177
## 10     3         1   0 149
## 11     4         1   0 125
## 12     5         1   0 110
## 13    10         1   0  91
## 14   100         1   2  47
## 15     1         2   0 200
## 16     2         2   0 177
## 17     3         2   0 149
## 18     4         2   0 125
## 19     5         2   0 110
## 20    10         2   0  91
## 21   100         2   2  47
## 22     1         3   0 200
## 23     2         3   0 177
## 24     3         3   0 149
## 25     4         3   0 125
## 26     5         3   0 110
## 27    10         3   0  91
## 28   100         3   2  47
## 29     1         4   0 200
## 30     2         4   0 177
## 31     3         4   0 149
## 32     4         4   0 125
## 33     5         4   0 110
## 34    10         4   0  91
## 35   100         4   2  47
## 36     1         5   0 200
## 37     2         5   0 177
## 38     3         5   0 149
## 39     4         5   0 125
## 40     5         5   0 110
## 41    10         5   0  91
## 42   100         5   2  47
## 43     1         6   0 200
## 44     2         6   0 177
## 45     3         6   0 149
## 46     4         6   0 125
## 47     5         6   0 110
## 48    10         6   0  91
## 49   100         6   2  47
## 50     1         7   0 200
## 51     2         7   0 177
## 52     3         7   0 149
## 53     4         7   0 125
## 54     5         7   0 110
## 55    10         7   0  91
## 56   100         7   2  47
## 57     1         8   0 200
## 58     2         8   0 177
## 59     3         8   0 149
## 60     4         8   0 125
## 61     5         8   0 110
## 62    10         8   0  91
## 63   100         8   2  47
## 64     1         9   0 200
## 65     2         9   0 177
## 66     3         9   0 149
## 67     4         9   0 125
## 68     5         9   0 110
## 69    10         9   0  91
## 70   100         9   2  47
## 71     1        10   0 200
## 72     2        10   0 177
## 73     3        10   0 149
## 74     4        10   0 125
## 75     5        10   0 110
## 76    10        10   0  91
## 77   100        10   2  47

adapting threshold

grid <- expand.grid(sigma = c(1,2,3,4,5,10,100),
             offset = c(100,10,1,0,0.01,0.001,0.0001,0.00001,0.000001,0.0000001),
             w=c(1,5,10,50))

img2 <- image@grey * 255
# Set up plots for 96 dpi images
  #library(EBImage)
  #dm <- dim(img2)/96
  #dev.new(width = dm[1], height = dm[2])
# Low pass filter with gblur and make binary

for(i in 1:dim(grid)[1]){

    writeLines(c(
    paste0("- sigma: ",grid$sigma[i]),
    paste0("- offset: ", grid$offset[i]),
    paste0(" - w and h: ",grid$w[i])
  ))

  #Low-pass Gaussian filter
  xb <- EBImage::gblur(img2, grid$sigma[i])
  plot(xb)

  image2 <- pixmapGrey(xb,cellres=c(1,1)) #(xb-min(xb))/(max(xb)-min(xb))

  plot(image2)


  xt <- EBImage::thresh(xb, w=grid$w[i],
                        h=grid$w[i],
                        offset = grid$offset[i])
  plot(xt)

  image3 <- image
  image3@grey <- xt

  plot(image3)
}

image_line_segment_detector

x <- image@grey * 255

linesegments <- image_line_segment_detector(x,
                                            scale = 0.8,
                                            sigma_scale = 0.6,
                                            quant = 2,
                                            ang_th = 22.5,
                                            log_eps = 0,
                                            density_th = 0.7,
                                            n_bins = 1024,
                                            union = FALSE,
                                            union_min_length = 5,
                                            union_max_distance = 5,
                                            union_ang_th = 7,
                                            union_use_NFA = FALSE,
                                            union_log_eps = 0)

linesegments
plot(image)
plot(linesegments, add = TRUE, col = "red")
scale = 0.1
linesegments <- image_line_segment_detector(x,
                                            scale = scale,
                                            sigma_scale = 0.6,
                                            quant = 2,
                                            ang_th = 22.5,
                                            log_eps = 0,
                                            density_th = 0.7,
                                            n_bins = 1024,
                                            union = FALSE,
                                            union_min_length = 5,
                                            union_max_distance = 5,
                                            union_ang_th = 7,
                                            union_use_NFA = FALSE,
                                            union_log_eps = 0)

linesegments
plot(image)
plot(linesegments, add = TRUE, col = "red")

quant

-default: 2 - no segments with quant 10 - quant 1: more detailed segments in tissue

quant = 1
linesegments <- image_line_segment_detector(x,
                                            scale = 0.8,
                                            sigma_scale = 0.6,
                                            quant = quant,
                                            ang_th = 22.5,
                                            log_eps = 0,
                                            density_th = 0.7,
                                            n_bins = 1024,
                                            union = FALSE,
                                            union_min_length = 5,
                                            union_max_distance = 5,
                                            union_ang_th = 7,
                                            union_use_NFA = FALSE,
                                            union_log_eps = 0)

linesegments
plot(image)
plot(linesegments, add = TRUE, col = "red")
  • quant 3: single details in tissue
quant = 3
linesegments <- image_line_segment_detector(x,
                                            scale = 0.8,
                                            sigma_scale = 0.6,
                                            quant = quant,
                                            ang_th = 22.5,
                                            log_eps = 0,
                                            density_th = 0.7,
                                            n_bins = 1024,
                                            union = FALSE,
                                            union_min_length = 5,
                                            union_max_distance = 5,
                                            union_ang_th = 7,
                                            union_use_NFA = FALSE,
                                            union_log_eps = 0)

linesegments
plot(image)
plot(linesegments, add = TRUE, col = "red")

ang_th

  • default: 22.5
  • ang_th 1: no lines segments
  • ang_th 100:
ang_th <- 100
linesegments <- image_line_segment_detector(x,
                                            scale = 0.8,
                                            sigma_scale = 0.6,
                                            quant = 2,
                                            ang_th = ang_th,
                                            log_eps = 0,
                                            density_th = 0.7,
                                            n_bins = 1024,
                                            union = FALSE,
                                            union_min_length = 5,
                                            union_max_distance = 5,
                                            union_ang_th = 7,
                                            union_use_NFA = FALSE,
                                            union_log_eps = 0)

linesegments
plot(image)
plot(linesegments, add = TRUE, col = "red")

log_eps

log_eps <- 10
linesegments <- image_line_segment_detector(x,
                                            scale = 0.8,
                                            sigma_scale = 0.6,
                                            quant = 2,
                                            ang_th = 22.5,
                                            log_eps = log_eps,
                                            density_th = 0.7,
                                            n_bins = 1024,
                                            union = FALSE,
                                            union_min_length = 5,
                                            union_max_distance = 5,
                                            union_ang_th = 7,
                                            union_use_NFA = FALSE,
                                            union_log_eps = 0)

linesegments
plot(image)
plot(linesegments, add = TRUE, col = "red")
library(pixmap)
library(magick)
imagelocation <- system.file("extdata", "chairs.pgm", package="image.LineSegmentDetector")
image <- read.pnm(file = imagelocation, cellres = 1)
x <- image@grey * 255

linesegments <- image_line_segment_detector(x,union=TRUE)
linesegments
plot(image)
plot(linesegments, add = TRUE, col = "red")

image_contour_detector

contourlines  <- image_contour_detector(x, Q = 2)
contourlines
plot(image)
plot(contourlines, add = TRUE, col = "red")

image_canny_edge_detector

edges <- image_canny_edge_detector(x)
edges
plot(edges)